/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>

#include "adc.h"
#include "beep.h"
#include "cmsis_os.h"
#include "fatfs.h"
#include "uart_printf.h"
#include "LED.h"
#include "flash.h"
#include "gui_include.h"
#include "lcd.h"
#include "light.h"
#include "netif.h"
#include "protocol.h"
#include "sdcardTest.h"
#include "sdio.h"
#include "stm32f4xx_hal_sd.h"
#include "touch.h"
#include "touchTask.h"
#include "usart.h"
#include "UserFat32.h"
#include "w25x16.h"
#include "webserver.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */


/* USER CODE END Variables */
osThreadId defaultTaskHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void const * argument);

extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 1024);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */


  touch_task_init();
  GUI_task_init();

  uart_task_init();
  led_task_init();

  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */


extern osMessageQId lightQueueHandle;
extern HAL_SD_CardInfoTypeDef pCardInfo;


// #define SRAM_TEST
#ifdef  SRAM_TEST
#define SRAM_BANK_ADDR (uint32_t )0X68000000
#endif

/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
  /* init code for LWIP */

  /* USER CODE BEGIN StartDefaultTask */
  webserver_init();

  /******** SRAM TEST ***********/


#ifdef  SRAM_TEST
  uint8_t *p;
  p = (uint8_t *)SRAM_BANK_ADDR;
  for(int i = 1;i < 10;i++)
  {
    *p = i;
    p++;
  }
  p = (uint8_t *)SRAM_BANK_ADDR;
  for(int i = 1;i < 10;i++)
  {
    debug_printf(DEBUG_LEVEL_VERBOSE, "[DEBUG_LEVEL_INFO] data : %d\r\n",*p);
    p++;
  }
  p = NULL;
#endif
  /******** SRAM TEST ***********/






  /******** light start ***********/
  light_task_init();
  /******** BEEP end ***********/



  /******** flash start ***********/
  // flash_init();
  // uint16_t id = 0;
  // id = sFLASH_ReadID();
  // debug_printf(DEBUG_LEVEL_VERBOSE, "[DEBUG_LEVEL_INFO] deviceid : %x\r\n",id);
  /******** flash end ***********/

  /******** SD CARD end ***********/
  // sdcardTest();
  // debug_printf(DEBUG_LEVEL_VERBOSE, "[DEBUG_LEVEL_INFO] SD Card TesT\r\n");
  // HAL_SD_GetCardInfo(&hsd,&pCardInfo);
  // debug_printf(DEBUG_LEVEL_VERBOSE, "[DEBUG_LEVEL_INFO] HAL_SD_GetCardInfo(&hsd,&pCardInfo)->%u\r\n",pCardInfo.BlockSize);
  /******** SD CARD end ***********/



  INFO("system running...");
  /* Infinite loop */
  for(;;)
  {
    // LED闪烁
    LED_TEST();
    osDelay(1000);
  }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */
